<?php

namespace App\Listeners;

use App\Libraries\LTLog;
use Illuminate\Database\Events\QueryExecuted;

class QueryListener
{
    /**
     * Create the event listener.
     *
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  QueryExecuted $event
     * @return void
     */
    public function handle(QueryExecuted $event)
    {
        //
        if ('local' == app()->environment()) {

            $logData = request()->logData;
            $globalId = isset($logData['globalid']) ? $logData['globalid'] : str_random(16);
            $originSQL = str_replace("?", "'%s'", $event->sql);
            $sql = vsprintf($originSQL, $event->bindings);
            $sqlLog = sprintf("%s\t%sms\t%s\r\n", $globalId, $event->time, $sql);
            LTLog::queryLogger()->addInfo($sqlLog);

        }
    }
}
